<template>
  <!-- 资讯分类管理 -->
  <div>
    <div>
      <div class="lunbotu">
        <el-button @click="toAdd" class="btn" type="primary" size="mini"
          >新增</el-button
        >
      </div>
    </div>
    <!-- 表格 -->
    <div>
      <el-table :data="tableData" size="mini" style="width: 100%">
        <el-table-column
          
          fixed
          type="index"
          align="center"
          label="序号"
          width="100"
        >
        </el-table-column>
        <el-table-column prop="name" align="center" label="名称">
        </el-table-column>
        <el-table-column prop="no" align="center" label="排序号">
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
        </el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              class="warning_info"
              icon="el-icon-edit-outline"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              class="red_info"
              icon="el-icon-delete"
              @click="toDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <el-form size="medium" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item
          prop="name"
          label="栏目名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="no" label="序号" :label-width="formLabelWidth">
          <el-input v-model.number="form.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="栏目介绍" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="form.description"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { findAll, deleteById, saveOrUpdate } from "@/api/infoCategory";

export default {
  data() {
    return {
      tableData: [],
      // 模态框的显示与隐藏
      visible: false,
      // 表单数据
      form: {},
      // 表单控件标题的宽度
      formLabelWidth: "80px",
      // 模态框的标题
      title: "",
      // 校验规则
      rules: {
        name: [
          {
            required: true,
            message: "请输入栏目名称",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {},
  methods: {
    async findAllData() {
      this.loading = true;
      let temp = {
        // ...this.params,
        // ...this.searchForm,
      };

      // 遍历对象
      for (let key in temp) {
        if (!temp[key]) {
          delete temp[key];
        }
      }
      let res = await findAll(temp);
      // 加载完毕
      this.loading = false;
      this.tableData = res.data;
      // this.total = res.data.total;
    },
    // 添加按钮事件处理程序
    toAdd() {
      this.findAllData();
      // 清空表单数据
      this.form = {};
      this.title = "新增栏目信息";
      this.visible = true;
    },
    toEdit(row) {
      this.findAllData();
      // 设置表单数据
      this.form = { ...row };
      this.title = "编辑栏目信息";
      this.visible = true;
    },
    toDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await deleteById({ id });
          this.$notify.success({
            title: "成功",
            message: res.success,
          });
          this.findAllData();
        })
        .catch(() => {
          this.$notify.info({
            title: "取消",
            message: "已取消删除",
          });
        });
    },
    // 模态框确定按钮事件处理程序
    toSave() {
      // 获取dom的值，对值进行校验 form.name
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = await saveOrUpdate(this.form);
          // 提示用户保存成功，刷新表格数据，关闭模态框
          this.$notify.success({
            title: "成功",
            message: res.message,
          });
          this.findAllData();
          // this.toClose();
          this.visible = false;
        } else {
          // 校验不通过
          return false;
        }
      });
    },
    // 关闭模态框
    toClose() {
      // 重置表单验证
      this.$refs.ruleForm.resetFields();
    },
  },
  created() {
    this.findAllData();
  },
  mounted() {},
};
</script>
<style scoped>
</style>